/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RecibosNumeracionIniciar.java
 *
 * Created on 03/16/2009, 11:04:15 AM
 */
package org.recibos.numeracion;
   
import org.mybeans.Sesion;
import org.mybeans.RecibesNumeracionBeans;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author avbravo
 */
public class RecibosNumeracionActualizarFrame extends javax.swing.JFrame {

    RecibesNumeracionBeans recibesNumeracionBeans;
    Sesion sesion;
    int Anio;

    /** Creates new form RecibosNumeracionIniciar */
    public RecibosNumeracionActualizarFrame() {
        initComponents();
        recibesNumeracionBeans = new RecibesNumeracionBeans();
        sesion = new Sesion();
        /*
         * Fondo
         */

        
          
         

        jXPanel1.setOpaque(false);
        setContentPane(jXPanel1);

        /*
         * fin del fondo
         */
         java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
         this.Anio = ca.get(java.util.Calendar.YEAR);


        int reciboinicial = recibesNumeracionBeans.ReciboInicial();
        if (reciboinicial == -1) {
            JOptionPane.showMessageDialog(this, "No se ha creado la numeracion  inicial de recibos .", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        } else {
            jTextFieldNumeracionInicial.setText(String.valueOf(reciboinicial));

            int recibo = recibesNumeracionBeans.ReciboUltimo();
            if (recibo != -1) {
                jTextFieldNumeracionOld.setText(String.valueOf(recibo));
                int nuevorecibo = recibo + 1;
                jTextFieldNumeracion.setText(String.valueOf(nuevorecibo));
                jButtonActualizar.setEnabled(true);
            } else {
                JOptionPane.showMessageDialog(this, "No se ha creado la numeracion  inicial de recibos .", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldNumeracionOld = new javax.swing.JTextField();
        jButtonCerrar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jButtonActualizar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldNumeracion = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldNumeracionInicial = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Actualizar Numeracion Recibos");

        jLabel1.setText("Ultimo Numero Recibo procesado");

        jTextFieldNumeracionOld.setEnabled(false);
        jTextFieldNumeracionOld.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldNumeracionOldActionPerformed(evt);
            }
        });

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonCerrar.setToolTipText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jLabel2.setText("A partir de este numero se generaran los nuevos  numeros de recibo");

        jButtonActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/save.png"))); // NOI18N
        jButtonActualizar.setMnemonic('g');
        jButtonActualizar.setToolTipText("Actualizar Registro");
        jButtonActualizar.setEnabled(false);
        jButtonActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonActualizarActionPerformed(evt);
            }
        });

        jLabel3.setText("Generar numeros a partir de ");

        jLabel4.setText("Primer Numero generado");

        jTextFieldNumeracionInicial.setEnabled(false);

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel2))
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addGap(2, 2, 2)
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTextFieldNumeracionInicial, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldNumeracionOld, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jButtonActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30)
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldNumeracion))))
                .addContainerGap())
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldNumeracionInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldNumeracionOld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldNumeracion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButtonActualizar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonCerrar, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldNumeracionOldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldNumeracionOldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jTextFieldNumeracionOldActionPerformed

    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jButtonActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonActualizarActionPerformed
        // TODO add your handling code here:
        try {

            String SidRecibo = jTextFieldNumeracion.getText();
            if (SidRecibo == null || SidRecibo.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el numero de recibo.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldNumeracion.requestFocus();
                return;
            }
            int idrecibo = Integer.parseInt(SidRecibo);
            String SidReciboOld = jTextFieldNumeracionOld.getText();
            if (SidReciboOld == null || SidReciboOld.equals("")) {
                JOptionPane.showMessageDialog(this, "No se especifico el ultimo recibo procesado.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldNumeracion.requestFocus();
                return;
            }
            int idreciboOld = Integer.parseInt(SidReciboOld);
            if (idrecibo < 0) {
                JOptionPane.showMessageDialog(this, "El numero de recibo no debe ser negativo.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldNumeracion.requestFocus();
                return;
            }

//            if (idrecibo < idreciboOld) {
//                JOptionPane.showMessageDialog(this, "El numero de recibo actual\nNo puede ser menor que el ultimo que se uso.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
//                jTextFieldNumeracion.requestFocus();
//              return;
//            }
           
            recibesNumeracionBeans.setActivo("si");
            recibesNumeracionBeans.setIdMunicipio(sesion.getIdmunicipio());
            recibesNumeracionBeans.setIdRecibo(idrecibo);
          
           boolean guardado = recibesNumeracionBeans.Guardar();
            if (guardado == true) {
                JOptionPane.showMessageDialog(this, "Numeracion de recibo guardada exitosamente.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jButtonActualizar.setEnabled(false);
               
            } else {
                JOptionPane.showMessageDialog(this, "No se pudo guardar la Numeracion de recibo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Clase:RecibosNumeracionIniciarFrame.java\nMetodo:JButtonGuardar()\nError: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_jButtonActualizarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {  
                new RecibosNumeracionActualizarFrame().setVisible(true);
            }
        });
    }

      private void Consultar() {
        try {
          
          
            int reciboinicial = recibesNumeracionBeans.ReciboInicial();
            if (reciboinicial == -1) {
                JOptionPane.showMessageDialog(this, "No se ha creado la numeracion  inicial de recibos .", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            } else {
                jTextFieldNumeracionInicial.setText(String.valueOf(reciboinicial));
            }
            int recibo = recibesNumeracionBeans.ReciboUltimo();
            if (recibo != -1) {
                jTextFieldNumeracionOld.setText(String.valueOf(recibo));
            } else {
                JOptionPane.showMessageDialog(this, "No se  creo la numeracion de recibos.\n Puede consultarla.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (Exception ex) {

            sesion.MostrarMensajeError("Error", "Clase:RecibesBeans.java\nMetodo:ReciboInicial() \nError: " + ex.getMessage().toString());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonActualizar;
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextFieldNumeracion;
    private javax.swing.JTextField jTextFieldNumeracionInicial;
    private javax.swing.JTextField jTextFieldNumeracionOld;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    // End of variables declaration//GEN-END:variables
}
